cc.FileUtils:getInstance():setPopupNotify(false)

cc.FileUtils:getInstance():addSearchPath("src/")
cc.FileUtils:getInstance():addSearchPath("res/")                              --后面       本地的
cc.FileUtils:getInstance():addSearchPath("res/cocos")
cc.FileUtils:getInstance():addSearchPath("res/data")
cc.FileUtils:getInstance():addSearchPath("src/packages/proto")

--这些不需要重新加载
require("socket")
require("zlib")
require("lfs")


----------------------------------先把C++那里加载的东西都保存下来
local loadAndPreload={}
for key, _ in pairs(package.loaded) do
    loadAndPreload[key]=true
end
for key, _ in pairs(package.preload) do
    loadAndPreload[key]=true
end
----------------------------------------------------

--预加载库
local function preloadLua()
    require("config")
    require("cocos.init")
end

preloadLua()

local FzFileUtils=require("src.app.public.FzFileUtils") --这个必须在preloadLua后执行，否则会报错

local ps=cc.FileUtils:getInstance():getSearchPaths()
local downloadPath = cc.FileUtils:getInstance():getWritablePath() .. "FZdownloaded"
--table.insert(ps,1,cc.FileUtils:getInstance():getWritablePath())
table.insert(ps,1,downloadPath)
table.insert(ps,1,downloadPath.."/src/")
table.insert(ps,1,downloadPath.."/res/")
table.insert(ps,1,downloadPath.."/res/cocos")
table.insert(ps,1,downloadPath.."/res/data")
table.insert(ps,1,downloadPath.."/src/packages/proto")         --前面    下载来的
cc.FileUtils:getInstance():setSearchPaths(ps)

local function ReloadLua()
    local loaded=package.loaded
    for key, _ in pairs(loaded) do
        if not loadAndPreload[key] then
            loaded[key]=nil
            print("卸载"..key)
        end
    end
    local preload=package.preload
    for key, _ in pairs(preload) do
        if not loadAndPreload[key] then
            preload[key]=nil
            print("卸载"..key)
        end
    end
    cc.work_global()
    preloadLua()    --重新加载预加载库
    cc.disable_global()
end
cc.exports.ReloadLua=ReloadLua

ReloadLua() --这里执行先一遍，把FZdownloaded目录下的文件加载起来

require("app.public.GlobalHelper")  --在FzInit中会重新载入这个文件


-- 打印出当前的searchpath
local function LogSearchPath(flag)
	print(string.format("========================SearchPaths %s=========================", flag))
	local searchPaths = cc.FileUtils:getInstance():getSearchPaths()
	for i,v in ipairs(searchPaths) do
		print(v)
	end
	print("==========================SearchPaths===========================")
end

local function main()
    LogSearchPath()
    local myapp=require("app.MyApp"):create()
    cc.exports.MyApp=myapp
    FzFileUtils.deleteOldFiles()                            --删除旧的客户端的资源,FzFileUtils是在上面缓存的，不受ReloadLua影响

    myapp:run("LaunchScene")
end


local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
    print(msg)
end

